﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PearTunes.Model;
using PearTunes.DAL;

namespace PearTunes.WEB.Models
{
    /// <summary>
    /// Class for accessing the music entities
    /// </summary>
    public class MusicModel
    {
        private readonly Random _rng = new Random();
        private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        /// <summary>
        /// Generates a random file name
        /// </summary>
        /// <param name="size">The length of the generated string</param>
        /// <returns>Random string</returns>
        public string GenerateRandomFileName(int size = 30)
        {

            char[] buffer = new char[size];

            for (int i = 0; i < size; i++)
            {
                buffer[i] = _chars[_rng.Next(_chars.Length)];
            }
            return new string(buffer);

        }
        /// <summary>
        /// Creates a new track
        /// </summary>
        /// <param name="t">The track entity that will be generated</param>
        public void NewTrack(Track t)
        {
            PearTunesEntities context = new PearTunesEntities();
            if (t.Albums != null && t.Albums.Count > 0)
            {
                List<string> albumsToUpdateInTrack = new List<string>();
                foreach (Album a in t.Albums)
                {
                    if ((from album in context.Albums where album.Title == a.Title select album).Count() == 0)
                    {
                        if (!a.Tracks.Contains(t))
                            a.Tracks.Add(t);
                        context.Albums.Add(a);
                    }
                    else
                    {
                        Album alreadyIn = context.Albums.Include("Tracks").Where(alb => alb.Title == a.Title).First();
                        if (alreadyIn.Tracks == null)
                            alreadyIn.Tracks = new List<Track>();
                        alreadyIn.Tracks.Add(t);
                        albumsToUpdateInTrack.Add(a.Title);
                    }
                }
                foreach (string s in albumsToUpdateInTrack)
                {
                    t.Albums.Remove(t.Albums.Where(alb => alb.Title == s).First());
                    t.Albums.Add(context.Albums.Include("Tracks").Where(album => album.Title == s).First());
                }
            }
            //context.Tracks.Add(t); //nemkell
            context.SaveChanges();
        }
        /// <summary>
        /// Deletes a track
        /// </summary>
        /// <param name="trackId">Deletes a track with the matching ID</param>
        public void DeleteTrack(int trackId)
        {
            PearTunesEntities context = new PearTunesEntities();
            IEnumerable<Track> toDelete = context.Tracks.Where(t => t.TrackId == trackId);
            if(toDelete.Count() > 0)
                context.Tracks.Remove(toDelete.First());
            context.SaveChanges();
        }
        /// <summary>
        /// Lists all the tracks from the database
        /// </summary>
        /// <returns>Collection of track entities</returns>
        public IList<Track> ListTracks()
        {

            //return (new PearTunesService()).GetTracks().ToList();
            return (new PearTunesEntities()).Tracks.Include("Albums").ToList();
        }
        /// <summary>
        /// Lists all the albums from the database
        /// </summary>
        /// <returns>Collection of album entities</returns>
        public IList<Album> ListAlbums()
        {
            return (new PearTunesEntities()).Albums.Include("Tracks").ToList();
        }
        /// <summary>
        /// Searches and album by album title
        /// </summary>
        /// <param name="title">The title of the album</param>
        /// <returns>The first matching album</returns>
        public Album SearchForAlbum(string title)
        {
            IQueryable<Album> result = from a in (new PearTunesEntities()).Albums where a.Title == title select a;
            return result.Count() > 0 ? result.First() : null;
        }
        /// <summary>
        /// Generates a unique ID for albums
        /// </summary>
        /// <returns>Unique album ID</returns>
        public int GenerateAlbumId()
        {
            return (new PearTunesEntities()).GenerateAlbumId();
            //if ((new PearTunesEntities()).Albums.Count() > 0)
            //    return (from a in (new PearTunesEntities()).Albums orderby a.AlbumId descending select a).First().AlbumId + 1;
            //else return 0;
        }
        /// <summary>
        /// Generates a unique ID for tracks
        /// </summary>
        /// <returns>Unique track ID</returns>
        public int GenerateTrackId()
        {
            return (new PearTunesEntities()).GenerateTrackId();
            //if ((new PearTunesEntities()).Tracks.Count() > 0)
            //    return (from a in (new PearTunesEntities()).Tracks orderby a.TrackId descending select a).First().TrackId + 1;
            //else return 0;
        }


    }
}
